import random
import sys
import pygame
from pygame.locals import *
from bullet2 import Bullet2
from game import Game
from bullet import Bullet
from player1 import Player1
from player2 import Player2
from wall import Wall
from item import Item
from monster import Monster
from button import Button
from text import Text
from image import Image
from music import Music


def game_star():
    pygame.init()
    # , FULLSCREEN | HWSURFACE, 32
    Game.window = pygame.display.set_mode((Game.W, Game.H))

    Game.bg = pygame.image.load("img/levelblack.jpg")
    Background_img_rect = Game.bg.get_rect()
    Game.music = pygame.mixer.Sound("wav/游戏.mp3")
    Game.music.play(-1)

    Game.player1_x = 230
    Game.player1_y = 500
    Game.player1 = Player1(Game.player1_x, Game.player1_y)
    Game.playerGroup = pygame.sprite.Group()
    Game.playerGroup.add(Game.player1)

    Game.player2_x = 320
    Game.player2_y = 500
    Game.player2 = Player2(Game.player2_x, Game.player2_y)
    Game.playerGroup2 = pygame.sprite.Group()
    Game.playerGroup2.add(Game.player2)

    Game.bullet = Bullet(0, 0, "left", 10)
    Game.item = Item(-100, -100, [0, -5], "left")
    Game.monster = Monster(0, 0, 6, 60)
    Game.current_time = pygame.time.get_ticks()
    Game.last_time = pygame.time.get_ticks()
    Game.player1_gangqiang = 5
    Game.player2_gangqiang = 5
    Game.ta_xue = 500
    Game.wall = Wall()
    Game.level_text = "关卡1"
    # [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    # [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    # [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
    # [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
    Game.wall_list = [
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
        [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
    ]
    # color
    Game.Black = (0, 0, 0)
    Game.White = (255, 255, 255)

    # 音效
    Game.fashe_music = Music("Wav/发射.wav")
    Game.fashe_music2 = Music("Wav/仍手雷.mp3")
    Game.jump_music = Music("wav/跳.wav")
    Game.daojishi_music = Music("wav/10倒计时.mp3")
    Game.jiangshilailin_music = Music("wav/僵尸来了.mp3")
    Game.monster_hit_music = Music("wav/怪物死.wav")
    Game.renwu_hit_music = Music("wav/死声.wav")
    Game.pengzhuang_music = Music("wav/碰撞.wav")
    Game.chibaoshi_music = Music("wav/吃宝石.wav")
    Game.boss_music = Music("wav/boss来临.mp3")
    Game.boss = True
    # text

    Game.score_text1 = Text("得分:" + str(Game.player1_scoer), (255, 200, 0), 20, 70, 40)
    Game.score_text2 = Text("得分:" + str(Game.player2_scoer), (55, 255, 0), 250, 70, 40)
    Game.score_renwu1 = Text("任务1:保护大本营", (0, 0, 0), 1280, 30, 30)
    Game.score_renwu2 = Text("任务2:击杀250个怪物/" + str(Game.monster_pengzhuang_si), (0, 0, 0), 1280, 80, 30)
    Game.score_renwu3 = Text("任务3:击败BOSS", (0, 0, 0), 1280, 130, 30)
    Game.score_renwu4 = Text("任务4:坚持250秒", (0, 0, 0), 1280, 180, 30)
    Game.warning = Text("注意事项:不要人物所在地方放箱子！", (0, 0, 0), 10, 850, 30)

    Game.player1_qiang = Text("X" + str(Game.player1_gangqiang), (0, 0, 0), 70, 130, 30)
    Game.player2_qiang = Text("X" + str(Game.player2_gangqiang), (0, 0, 0), 300, 130, 30)
    Game.player1_qiang2 = Text("X无限", (0, 0, 0), 170, 130, 30)
    Game.player2_qiang2 = Text("X无限", (0, 0, 0), 400, 130, 30)
    Game.player1_text1 = Text("J", (0, 0, 0), 35, 132, 30)
    Game.player2_text2 = Text("K", (0, 0, 0), 130, 130, 30)
    Game.player1_jineng_text1 = Text("0", (0, 0, 0), 262, 132, 30)
    Game.player2_jineng_text2 = Text("1", (0, 0, 0), 362, 130, 30)

    Game.ta = Image("img/塔.png", -150, 250, 300, 500, 255)
    Game.ta_rect = pygame.Rect(-150, 250, 200, 500)
    Game.ta2 = Image("img/塔2.png", 470, 10, 150, 150, 255)
    Game.ta_hp = Text("大本营HP：" + str(Game.ta_xue), (0, 0, 200), 800, 60, 30)

    Game.game_time = Text("", Game.Black, 280, 200, 36)
    Game.text_texiao = Text("3", Game.Black, 600, -100, 800)

    Game.shijian_text = "加强你的防御，怪物即将来临，您只有30秒的时间："

    # 图标
    Game.tubiao_1_image = Image("img/图标黄色.png", 20 * 60, 10, 50, 50, 255)
    Game.tubiao_2_image = Image("img/图标绿色.png", 250 * 65, 10, 50, 50, 255)
    Game.chuansong_image = Image("img/传送门2.png", 1570, 230 * 150, 70, 70, 255)
    Game.qiang1 = Image("img/墙.png", 20, 130, 40, 40, 255)
    Game.qiang2 = Image("img/墙.png", 250, 130, 40, 40, 255)
    Game.qiang3 = Image("img/墙壁999.png", 120, 130, 40, 40, 255)
    Game.qiang4 = Image("img/墙壁999.png", 350, 130, 40, 40, 255)

    Game.button = Button("哈喽", (100, 100, 100), (200, 200, 200), 100, 100, 100, 100, 50)
    Game.image = Image("img/墙.png", 300, 300, 300, 300, 255)
    Game.zixinag_img = pygame.image.load("img/纸箱复制.png")

    Game.monster_shengcheng_shijian = 30
    Game.monster_number = 50

    FPS = 60
    Game.fps = 0
    clock = pygame.time.Clock()
    Game.start_time = 0

    while True:
        # 游戏参数

        Game.xiangzishuliang += 1
        Game.xiangzishuliang2 += 1
        Game.index += 1
        Game.monster_shu += 1
        get_event()

        Game.window.blit(Game.bg, Background_img_rect)
        Game.wall.wall_display()
        # 游戏文字
        text_png()
        shijian()

        if Game.shengli:
            Game.playerGroup.update(Game.index)
            Game.playerGroup.draw(Game.window)
            Game.player1.confine()
            Game.player1.left_right()
            Game.player1.hit_wall()

            Game.playerGroup2.update(Game.index)
            Game.playerGroup2.draw(Game.window)
            Game.player2.confine()
            Game.player2.left_right()
            Game.player2.hit_wall()

        # 游戏逻辑
        game_logic()

        # 子弹刷新
        player1_box_collderect_monster()
        player2_bullet_collderect_monster()
        # 重力
        Game.player1.player1_G()
        Game.player2.player2_G()

        # 人物范围
        player_recur()
        # 人物与地面碰撞
        player1_collderect_ground()
        player2_collderect_ground()
        # 画面刷新

        pygame.display.set_caption("零零守卫者 FPS = {:.2f}".format(Game.fps))
        pygame.display.update()
        clock.tick(FPS)
        Game.fps = clock.get_fps()


def get_event():
    for event in pygame.event.get():
        # mouse_x, mouse_y = pygame.mouse.get_pos()
        # print(mouse_x, mouse_y)
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_r:
                Game.music.stop()
                Game().__init__()
                game_star()

            # Game.player1
            if event.key == pygame.K_w and Game.jump <= 1:
                Game.jump += 1
                Game.G_speed = -Game.speed
                Game.jump_music.play()

                Game.direction_W = True
                if Game.jump >= 3:
                    Game.jump = 0

            if event.key == pygame.K_a:
                Game.direction_A = True
                Game.current_images = Game.player1.images_left

            if event.key == pygame.K_d:
                Game.direction_D = True
                Game.current_images = Game.player1.images_right

            if event.key == pygame.K_j:
                Game.fangkuai = True

            if event.key == pygame.K_k:
                Game.fangkuai_qiang = True

            if event.key == pygame.K_s and Game.xiangzishuliang >= 8:
                # 鼠标按下时，设置物品的初始位置和速度
                Game.fashe_music2.play()
                Game.item.velocity = [0, -5]  # 设置初始速度
                if Game.direction == "left":
                    item_left = Item(Game.player1.rect.x, Game.player1.rect.y, Game.item.velocity, "left")
                    Game.item_list.append(item_left)
                    Game.xiangzishuliang = 0

                elif Game.direction == "right":
                    item_right = Item(Game.player1.rect.x, Game.player1.rect.y, Game.item.velocity, "right")
                    Game.item_list.append(item_right)
                    Game.xiangzishuliang = 0

            if event.key == pygame.K_UP and Game.jump2 <= 1:
                Game.jump2 += 1
                Game.G_speed2 = -Game.speed2
                Game.direction_W2 = True
                Game.jump_music.play()
                Game.direction_2 = True
                if Game.jump2 >= 3:
                    Game.jump2 = 0

            if event.key == pygame.K_DOWN and Game.xiangzishuliang2 > 8:
                Game.fashe_music.play()
                if Game.direction2 == "left":
                    bullet_right = Bullet2(Game.player2.rect.left, Game.player2.rect.top + 20, "left", 15)
                    Game.bullet_list.append(bullet_right)
                    Game.xiangzishuliang2 = 0

                elif Game.direction2 == "right":
                    bullet_left = Bullet2(Game.player2.rect.left, Game.player2.rect.top + 20, "right", 15)
                    Game.bullet_list.append(bullet_left)
                    Game.xiangzishuliang2 = 0

            if event.key == pygame.K_LEFT:
                Game.direction_A2 = True

            if event.key == pygame.K_RIGHT:
                Game.direction_D2 = True

            if event.key == pygame.K_KP0:
                Game.fangkuai2 = True

            if event.key == pygame.K_SPACE:
                Game.kongge = True

            if event.key == pygame.K_KP1:
                Game.fangkuai2_qiang = True

        elif event.type == pygame.KEYUP:
            # Game.player1
            if event.key == pygame.K_a:
                Game.direction_A = False

            if event.key == pygame.K_d:
                Game.direction_D = False

            if event.key == pygame.K_j:
                Game.fangkuai = False

            if event.key == pygame.K_k:
                Game.fangkuai_qiang = False

            if event.key == pygame.K_LEFT:
                Game.direction_A2 = False

            if event.key == pygame.K_RIGHT:
                Game.direction_D2 = False

            if event.key == pygame.K_KP0:
                Game.fangkuai2 = False

            if event.key == pygame.K_KP1:
                Game.fangkuai2_qiang = False

            if event.key == pygame.K_SPACE:
                Game.kongge = False

            if event.key == pygame.K_p:
                chuangkou = open("data/窗口大小.txt", "w", encoding="utf-8")

                if Game.quanping:
                    Game.window = pygame.display.set_mode((Game.W, Game.H), FULLSCREEN | HWSURFACE, 32)
                    chuangkou.write("全屏模式")
                    chuangkou.close()
                    Game.quanping = False
                else:
                    Game.window = pygame.display.set_mode((Game.W, Game.H))
                    Game.quanping = True
                    chuangkou.write("窗口模式")
                    chuangkou.close()


def shijian():
    # 获取当前时间
    current_time = pygame.time.get_ticks()
    # 计算经过的时间
    elapsed_time = current_time - Game.start_time
    # 转换为秒
    Game.seconds = elapsed_time / 1000.0 + 19


def text_png():
    for i in range(0, Game.player1_ming):
        Game.tubiao_1_image.display_image_star(20 + i * 60, 10, 50, 50, 255)

    for i in range(0, Game.player2_ming):
        Game.tubiao_2_image.display_image_star(250 + i * 65, 10, 50, 50, 255)

    for i in range(0, 4):
        Game.chuansong_image.display_image_star(1570, 230 + i * 150, 70, 70, 255)

    Game.qiang1.display_image()
    Game.qiang2.display_image()
    Game.qiang3.display_image()
    Game.qiang4.display_image()

    Game.player1_qiang.display_text_star("X" + str(Game.player1_gangqiang), 70, 130, 30)
    Game.player2_qiang.display_text_star("X" + str(Game.player2_gangqiang), 300, 130, 30)

    Game.player1_qiang2.display_text()
    Game.player2_qiang2.display_text()

    Game.player1_text1.display_text()
    Game.player2_text2.display_text()

    Game.player1_jineng_text1.display_text()
    Game.player2_jineng_text2.display_text()

    Game.ta.display_image()
    Game.ta2.display_image()
    Game.window.fill(Game.Black, (640, 45, 520, 70))
    Game.window.fill(Game.White, (650, 55, 500, 50))
    Game.window.fill((255, 255, 150), (650, 55, Game.ta_xue, 50))
    Game.ta_hp.display_text_star("大本营HP：" + str(Game.ta_xue), 800, 60, 30)

    Game.score_text1.display_text_star("得分:" + str(Game.player1_scoer), 20, 70, 30)
    Game.score_text2.display_text_star("得分:" + str(Game.player2_scoer), 250, 70, 30)
    Game.score_renwu1.display_text()
    Game.score_renwu2.display_text_star("任务2:击杀250个怪物/" + str(Game.monster_pengzhuang_si), 1280, 80, 30)
    Game.score_renwu3.display_text()
    Game.score_renwu4.display_text()
    Game.warning.display_text()


def game_logic():
    # 怪物生成
    if 22.05 >= Game.seconds >= 22:
        Game.daojishi_music.play()
    elif 30.05 >= Game.seconds >= 30:
        Game.jiangshilailin_music.play()
    elif Game.seconds >= 30:
        Game.monster_shuliang = len(Game.monster_list)
        if Game.monster_shu >= Game.monster_shengcheng_shijian and Game.monster_shuliang < Game.monster_number \
                and Game.seconds < 240:
            monster_suiji = random.randrange(0, 4) * 150
            monster = Monster(1650, 230 + monster_suiji, 0, 60)
            Game.monster_list.append(monster)
            Game.monster_shu = 0

        elif Game.seconds > 240 and Game.boss:
            Game.boss_music.play()
            # self.box_w = 2000
            monster = Monster(1650, 230 + 1 * 150, 0, 2000)
            Game.monster_list.append(monster)
            Game.monster_shu = 0
            Game.boss = False
        if 243 <= Game.seconds <= 243.02:
            Game.boss_music.play()
        if 246 <= Game.seconds <= 246.02:
            Game.boss_music.play()

        player_collderect_monster()
        monster_collderect_ta()
        monster_collderect_ground()

    if Game.seconds < 27:
        Game.game_time.display_text_star("{}{:.2f}秒".format(Game.shijian_text, Game.seconds), 280, 200, 40)

    elif 27 < Game.seconds < 250:
        Game.shijian_text = "时间："
        Game.game_time.display_text_star("{}{:.2f}秒".format(Game.shijian_text, Game.seconds), 20, 200, 30)
    elif Game.seconds >= 250:
        Game.seconds = 250

    if 27 <= Game.seconds <= 28:
        Game.text_texiao.display_text_star("3", 650, -50, 800)
    elif 28 <= Game.seconds <= 29:
        Game.text_texiao.display_text_star("2", 650, -50, 800)
    elif 29 <= Game.seconds <= 30:
        Game.text_texiao.display_text_star("1", 650, -50, 800)
    elif 30 <= Game.seconds <= 31:
        Game.text_texiao.display_text_star("开始！", 450, 200, 400)


def monster_collderect_ta():
    for monster in Game.monster_list:
        monster.monster_xinxi()
        monster.monster_go()
        monster.monster_display()
        if Game.ta_rect.colliderect(monster.Monster_rect):
            Game.ta_xue -= 50
            Game.monster_list.remove(monster)
        if monster.box_w <= 0:
            Game.monster_list.remove(monster)
            Game.monster_pengzhuang_si += 1


def player_collderect_monster():
    for monster in Game.monster_list:
        monster.monster_xinxi()
        monster.monster_go()
        monster.monster_display()

        if Game.player1.rect.colliderect(monster.Monster_rect):
            if monster.Monster_speed == 1:
                Game.monster_pengzhuang_si += 1
                monster.box_w -= 60
                Game.monster_hit_music.play()
                Game.player1.box_w -= 30
                Game.player1.rect.x = 250

            elif monster.Monster_speed == 2:
                Game.monster_pengzhuang_si += 1
                monster.box_w -= 40
                Game.monster_hit_music.play()
                Game.player1.box_w -= 30
                Game.player1.rect.x = 250

            elif monster.Monster_speed == 3:
                Game.monster_pengzhuang_si += 1
                monster.box_w -= 30
                Game.monster_hit_music.play()
                Game.player1.box_w -= 30
                Game.player1.rect.x = 250

            elif monster.Monster_speed == 4:
                Game.monster_pengzhuang_si += 1
                monster.box_w -= 20
                Game.monster_hit_music.play()
                Game.player1.box_w -= 30
                Game.player1.rect.x = 250

            elif monster.Monster_speed == 5:
                Game.monster_pengzhuang_si += 1
                monster.box_w -= 10
                Game.monster_hit_music.play()
                Game.player1.box_w -= 30
                Game.player1.rect.x = 250

        if Game.player2.rect.colliderect(monster.Monster_rect):
            if monster.Monster_speed == 1:
                Game.monster_pengzhuang_si += 1
                monster.box_w -= 60
                Game.monster_hit_music.play()
                Game.player2.box_w -= 30
                Game.player2.rect.x = 250

            elif monster.Monster_speed == 2:
                Game.monster_pengzhuang_si += 1
                monster.box_w -= 40
                Game.monster_hit_music.play()
                Game.player2.box_w -= 30
                Game.player2.rect.x = 250

            elif monster.Monster_speed == 3:
                Game.monster_pengzhuang_si += 1
                monster.box_w -= 30
                Game.monster_hit_music.play()
                Game.player2.box_w -= 30
                Game.player2.rect.x = 250

            elif monster.Monster_speed == 4:
                Game.monster_pengzhuang_si += 1
                monster.box_w -= 20
                Game.monster_hit_music.play()
                Game.player2.box_w -= 30
                Game.player2.rect.x = 250

            elif monster.Monster_speed == 5:
                Game.monster_pengzhuang_si += 1
                monster.box_w -= 10
                Game.monster_hit_music.play()
                Game.player2.box_w -= 30
                Game.player2.rect.x = 250


def player1_collderect_ground():
    for row in range(Game.MAP_HEIGHT):
        for col in range(Game.MAP_WIDTH):
            wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE, Game.BLOCK_SIZE,
                                    Game.BLOCK_SIZE)

            if Game.wall_list[row][col] == 0:
                wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE + 30, Game.BLOCK_SIZE,
                                        Game.BLOCK_SIZE - 30)
                if Game.direction == "right":
                    try:
                        if Game.player1.rect.colliderect(wall_rect):
                            if Game.player1.rect.left <= wall_rect.left:
                                Game.window.blit(Game.zixinag_img, (wall_rect.x + 50, wall_rect.y - 30))
                    except IndexError:
                        pass
                elif Game.direction == "left":
                    try:
                        if Game.player1.rect.colliderect(wall_rect):
                            if Game.player1.rect.right >= wall_rect.right:
                                Game.window.blit(Game.zixinag_img, (wall_rect.x - 50, wall_rect.y - 30))
                    except IndexError:
                        pass
                if Game.player1.rect.colliderect(
                        wall_rect) and Game.fangkuai and Game.direction == "right" and Game.xiangzishuliang >= 30:
                    try:
                        if Game.wall_list[row + 1][col + 2] == 0:
                            Game.wall_list[row + 1][col + 2] = 4
                            Game.xiangzishuliang = 0
                        Game.fangkuai = False

                    except IndexError:
                        Game.fangkuai = False
                elif Game.player1.rect.colliderect(
                        wall_rect) and Game.fangkuai and Game.direction == "left" and Game.xiangzishuliang >= 30:
                    try:
                        if Game.wall_list[row + 1][col - 1] == 0:
                            Game.wall_list[row + 1][col - 1] = 4
                            Game.xiangzishuliang = 0

                        Game.fangkuai = False
                    except IndexError:
                        Game.fangkuai = False
                # 刚体
                if Game.player1.rect.colliderect(
                        wall_rect) and Game.fangkuai_qiang and Game.direction == "right" and Game.xiangzishuliang >= 30:
                    try:
                        if Game.wall_list[row + 1][col + 2] == 0 and 0 < Game.player1_gangqiang <= 5:
                            Game.wall_list[row + 1][col + 2] = 1
                            Game.xiangzishuliang = 0
                            Game.player1_gangqiang -= 1
                        Game.fangkuai_qiang = False
                    except IndexError:
                        Game.fangkuai_qiang = False
                elif Game.player1.rect.colliderect(
                        wall_rect) and Game.fangkuai_qiang and Game.direction == "left" and Game.xiangzishuliang >= 30:
                    try:
                        if Game.wall_list[row + 1][col - 1] == 0 and 0 < Game.player1_gangqiang <= 5:
                            Game.wall_list[row + 1][col - 1] = 1
                            Game.xiangzishuliang = 0
                            Game.player1_gangqiang -= 1
                        Game.fangkuai_qiang = False
                    except IndexError:
                        Game.fangkuai_qiang = False

            if Game.wall_list[row][col] in (1, 2, 4):
                if Game.player1.rect.colliderect(wall_rect):
                    if Game.player1.rect.bottom >= wall_rect.top:
                        Game.player1.rect.y = wall_rect.y - Game.player1.rect.height
                        Game.G_speed = 0  # 碰撞后重置垂直速度
                        Game.direction_W = False
                        Game.jump = 0

                for item in Game.item_list:
                    if item.rect.colliderect(wall_rect):
                        Game.item_list.remove(item)
                        Game.pengzhuang_music.play()
                        if Game.wall_list[row][col] == 4:
                            Game.wall_list[row][col] = 0

            if Game.wall_list[row][col] == 3:
                wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE + 30, Game.BLOCK_SIZE,
                                        Game.BLOCK_SIZE - 30)

                if Game.player1.rect.colliderect(wall_rect):
                    Game.renwu_hit_music.play()
                    Game.player1.rect.x = Game.player1_x
                    Game.player1.rect.y = Game.player1_y
                    Game.player1.box_w = 100
                    Game.player1_ming -= 1


def player2_collderect_ground():
    for row in range(Game.MAP_HEIGHT):
        for col in range(Game.MAP_WIDTH):
            wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE, Game.BLOCK_SIZE,
                                    Game.BLOCK_SIZE)

            if Game.wall_list[row][col] == 0:
                wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE + 30, Game.BLOCK_SIZE,
                                        Game.BLOCK_SIZE - 30)
                # Player2墙图标预显示
                if Game.direction2 == "right":
                    try:
                        if Game.player2.rect.colliderect(wall_rect):
                            if Game.player2.rect.left <= wall_rect.left:
                                Game.window.blit(Game.zixinag_img, (wall_rect.x + 50, wall_rect.y - 30))
                    except IndexError:
                        pass
                elif Game.direction2 == "left":
                    try:
                        if Game.player2.rect.colliderect(wall_rect):
                            if Game.player2.rect.right >= wall_rect.right:
                                Game.window.blit(Game.zixinag_img, (wall_rect.x - 50, wall_rect.y - 30))
                    except IndexError:
                        pass

                # Player2放墙
                if Game.player2.rect.colliderect(
                        wall_rect) and Game.fangkuai2 and Game.direction2 == "right" and Game.xiangzishuliang2 >= 30:
                    try:
                        if Game.wall_list[row + 1][col + 2] == 0:
                            Game.wall_list[row + 1][col + 2] = 4
                            Game.xiangzishuliang2 = 0
                        Game.fangkuai2 = False

                    except IndexError:
                        Game.fangkuai2 = False

                elif Game.player2.rect.colliderect(
                        wall_rect) and Game.fangkuai2 and Game.direction2 == "left" and Game.xiangzishuliang2 >= 30:
                    try:
                        if Game.wall_list[row + 1][col - 1] == 0:
                            Game.wall_list[row + 1][col - 1] = 4
                            Game.xiangzishuliang2 = 0
                        Game.fangkuai2 = False
                    except IndexError:
                        Game.fangkuai2 = False

                # 刚体
                if Game.player2.rect.colliderect(
                        wall_rect) and Game.fangkuai2_qiang and Game.direction2 == "right" and \
                        Game.xiangzishuliang2 >= 30:
                    try:
                        if Game.wall_list[row + 1][col + 2] == 0 and 0 < Game.player2_gangqiang <= 5:
                            Game.wall_list[row + 1][col + 2] = 1
                            Game.xiangzishuliang2 = 0
                            Game.player2_gangqiang -= 1
                        Game.fangkuai2_qiang = False
                    except IndexError:
                        Game.fangkuai2_qiang = False

                elif Game.player2.rect.colliderect(wall_rect) and Game.fangkuai2_qiang and Game.direction2 == "left" \
                        and Game.xiangzishuliang2 >= 30:

                    try:
                        if Game.wall_list[row + 1][col - 1] == 0 and 0 < Game.player2_gangqiang <= 5:
                            Game.wall_list[row + 1][col - 1] = 1
                            Game.xiangzishuliang2 = 0
                            Game.player2_gangqiang -= 1
                        Game.fangkuai2_qiang = False
                    except IndexError:
                        Game.fangkuai2_qiang = False

            if Game.wall_list[row][col] in (1, 2, 4):
                if Game.player2.rect.top <= wall_rect.bottom:
                    if Game.player2.rect.colliderect(wall_rect):
                        Game.player2.rect.y = wall_rect.y - Game.player2.rect.height
                        Game.G_speed2 = 0  # 碰撞后重置垂直速度
                        Game.direction_W2 = False
                        Game.jump2 = 0

                for bullet2 in Game.bullet_list:
                    if bullet2.Bullet_rect.colliderect(wall_rect):
                        Game.bullet_list.remove(bullet2)
                        Game.pengzhuang_music.play()
                        if Game.wall_list[row][col] == 4:
                            Game.wall_list[row][col] = 0

            elif Game.wall_list[row][col] == 3:
                wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE + 30, Game.BLOCK_SIZE,
                                        Game.BLOCK_SIZE - 30)
                if Game.player2.rect.colliderect(wall_rect):
                    Game.renwu_hit_music.play()
                    Game.player2.rect.x = Game.player2_x
                    Game.player2.rect.y = Game.player2_y
                    Game.player2.box_w = 100
                    Game.player2_ming -= 1


def player1_box_collderect_monster():
    for item in Game.item_list:
        item.bullet_fire()
        item.bullet_display()
        if item.rect.left <= 0 or item.rect.left >= Game.W or item.rect.top >= 900:
            Game.item_list.remove(item)
        # 子弹绘制
        for monster in Game.monster_list:
            if item.rect.colliderect(monster.Monster_rect):
                if monster.Monster_speed == 1:
                    item.rect.x = 1700
                    Game.monster_hit_music.play()
                    Game.pengzhuang_music.play()
                    monster.box_w -= 60
                    Game.player1_scoer += 10

                elif monster.Monster_speed == 2:
                    item.rect.x = 1700
                    Game.monster_hit_music.play()
                    Game.pengzhuang_music.play()
                    monster.box_w -= 40
                    Game.player1_scoer += 20

                elif monster.Monster_speed == 3:
                    item.rect.x = 1700
                    Game.monster_hit_music.play()
                    Game.pengzhuang_music.play()
                    monster.box_w -= 30
                    Game.player1_scoer += 30

                elif monster.Monster_speed == 4:
                    item.rect.x = 1700
                    Game.monster_hit_music.play()
                    Game.pengzhuang_music.play()
                    monster.box_w -= 20
                    Game.player1_scoer += 40

                elif monster.Monster_speed == 5:
                    item.rect.x = 1700
                    Game.monster_hit_music.play()
                    Game.pengzhuang_music.play()
                    monster.box_w -= 10
                    Game.player1_scoer += 50


def player2_bullet_collderect_monster():
    for bullet in Game.bullet_list:
        bullet.bullet_fire()
        bullet.bullet_display()
        if bullet.Bullet_rect.left <= 0 or bullet.Bullet_rect.left >= Game.W:
            Game.bullet_list.remove(bullet)
        for monster in Game.monster_list:
            if bullet.Bullet_rect.colliderect(monster.Monster_rect):
                if monster.Monster_speed == 1:
                    bullet.Bullet_rect.x = 1700
                    Game.monster_hit_music.play()
                    Game.pengzhuang_music.play()
                    monster.box_w -= 60
                    Game.player2_scoer += 10

                elif monster.Monster_speed == 2:
                    bullet.Bullet_rect.x = 1700
                    Game.monster_hit_music.play()
                    Game.pengzhuang_music.play()
                    monster.box_w -= 40
                    Game.player2_scoer += 20

                elif monster.Monster_speed == 3:
                    bullet.Bullet_rect.x = 1700
                    Game.monster_hit_music.play()
                    Game.pengzhuang_music.play()
                    monster.box_w -= 30
                    Game.player2_scoer += 30

                elif monster.Monster_speed == 4:
                    bullet.Bullet_rect.x = 1700
                    Game.monster_hit_music.play()
                    Game.pengzhuang_music.play()
                    monster.box_w -= 20
                    Game.player2_scoer += 40

                elif monster.Monster_speed == 5:
                    bullet.Bullet_rect.x = 1700
                    Game.monster_hit_music.play()
                    Game.pengzhuang_music.play()
                    monster.box_w -= 10
                    Game.player2_scoer += 50


def monster_collderect_ground():
    for row in range(Game.MAP_HEIGHT):
        for col in range(Game.MAP_WIDTH):
            wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE, Game.BLOCK_SIZE,
                                    Game.BLOCK_SIZE)
            if Game.wall_list[row][col] in (1, 2, 4):
                for monster in Game.monster_list:
                    if wall_rect.colliderect(monster.Monster_rect):
                        if monster.Monster_speed == 1:
                            Game.wall_list[row][col] = 0
                            Game.monster_pengzhuang_si += 1
                            monster.box_w -= 60
                            Game.monster_hit_music.play()
                        elif monster.Monster_speed == 2:
                            Game.wall_list[row][col] = 0
                            Game.monster_pengzhuang_si += 1
                            Game.monster_hit_music.play()
                            monster.box_w -= 40
                        elif monster.Monster_speed == 3:
                            Game.wall_list[row][col] = 0
                            Game.monster_pengzhuang_si += 1
                            monster.box_w -= 30
                            Game.monster_hit_music.play()
                        elif monster.Monster_speed == 4:
                            Game.wall_list[row][col] = 0
                            Game.monster_pengzhuang_si += 1
                            monster.box_w -= 20
                            Game.monster_hit_music.play()
                        elif monster.Monster_speed == 5:
                            Game.wall_list[row][col] = 0
                            Game.monster_pengzhuang_si += 1
                            monster.box_w -= 10
                            Game.monster_hit_music.play()


def player_recur():
    if Game.player1.rect.y >= Game.H or Game.player2.rect.y >= Game.H:
        Game.renwu_hit_music.play()
        Game.music.stop()
        Game().__init__()
        game_star()

    if Game.player1.box_w <= 0:
        Game.renwu_hit_music.play()
        Game.player1.box_w = 100
        Game.player1.rect.x = Game.player1_x
        Game.player1.rect.y = Game.player1_y
        Game.player1_ming -= 1

    if Game.player2.box_w <= 0:
        Game.renwu_hit_music.play()
        Game.player2.box_w = 100
        Game.player2.rect.x = Game.player2_x
        Game.player2.rect.y = Game.player2_y
        Game.player2_ming -= 1

    # elif Game.player1_ming <= 0:
    #     Game.shengli = False
    #     font = pygame.font.Font("Font/font.ttf", 220)
    #     score_text = font.render(" 失败 ", True, (8, 25, 15), (255, 85, 0))
    #     score_text2 = font.render("  ", True, (8, 25, 15), (255, 85, 0))
    #     Game.window.blit(score_text, (50, 200))
    #     Game.window.blit(score_text2, (50, 450))
    #
    # elif Game.player2_ming <= 0:
    #     Game.shengli = False
    #     font = pygame.font.Font("Font/font.ttf", 220)
    #     score_text = font.render(" 失败 ", True, (8, 25, 15), (255, 85, 0))
    #     score_text2 = font.render("  ", True, (8, 25, 15), (255, 85, 0))
    #     Game.window.blit(score_text, (50, 200))
    #     Game.window.blit(score_text2, (50, 450))
    # else:
    #     Game.shengli = True


# 人物重力


if __name__ == '__main__':
    Game().__init__()
    game_star()
